package com.newhis.controller.thread;

import com.alibaba.fastjson.JSON;
import com.newhis.utils.FileUtils;
import org.apache.commons.lang3.time.DateFormatUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
import java.util.Date;
import java.util.Map;

@RestController
@RequestMapping("fileupload")
public class FileUploadController {
    private static final Logger log = LoggerFactory.getLogger(FileUploadController.class);
    @PostMapping("/upload")
    public ResponseEntity<?> upload(@RequestParam(value = "file") MultipartFile file, @RequestParam("partNumber") int partNumber){
        // 获取文件名
        String fileName = file.getOriginalFilename();
        //minio路径
        String path = DateFormatUtils.format(new Date(), "yyyyMMdd");
        String contentType = file.getContentType();
        Map<String, Object> result = FileUtils.initMultiPartUpload(path+"/"+fileName, partNumber, contentType);
        log.info("上传文件分片结果：{}", JSON.toJSONString(result));
        return ResponseEntity.ok(result);
    }

    @PostMapping("merge")
    public ResponseEntity<?> merge(@RequestParam("uploadId") String uploadId, @RequestParam("file") String file){
        //minio路径
        String path = DateFormatUtils.format(new Date(), "yyyyMMdd");
        boolean b = FileUtils.mergeMultiPartUpload(path+"/"+file, uploadId);
        return ResponseEntity.ok(b);
    }
}
